import Vue from 'vue'
import VueRouter from 'vue-router'
import AuthService from '@/api/authService'
// 路由数据
import routes from './routes.js'

Vue.use(VueRouter)

const router = new VueRouter(routes)

// TODO temp solution
const traineePath = {
    traineeHome: true,
    courseBusiness: true,
    typingTraining: true
}

const adminPath = {
    courseBusiness: true
}

/**
 * 路由钩子
 **/
router.beforeEach((to, from, next) => {
    if (AuthService.isLogin()) {
        // TODO temp

        if (traineePath[to.name] && AuthService.isTrainee()) {
            // 是trainee
            next()
        } else if (AuthService.isTrainee() && to.path == '/typingTraining') {
            // 是trainee访问非trainee目录
            next()
        } else if (!traineePath[to.name] && AuthService.isTrainee()) {
            // 是trainee访问非trainee目录
            next({ name: 'traineeHome' })
        } else if ((!traineePath[to.name] || adminPath[to.name]) && AuthService.isAdmin()) {
            // ADMIN 访问trainee目录
            next()
        } else if (!traineePath[to.name] && !AuthService.isTrainee()) {
            // 非trainee访问非trainee目录
            next()
        } else {
            next({ name: 'login' })
        }
    } else if (to.name !== 'login') {
        next({ name: 'login' })
    } else {
        next()
    }
})

router.afterEach((to, from) => {})

export default router
